﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.SqlServerDAL
{
    /// <summary>
    /// 类EnergyCalc。
    /// </summary>
    public class EnergyCalc
    {
        public EnergyCalc()
        { }
        #region Model
        private int _ec_id;
        private string _energyname;
        private decimal? _energyhot;
        private decimal _energyproportion;
        /// <summary>
        /// 流水号
        /// </summary>
        public int EC_ID
        {
            set { _ec_id = value; }
            get { return _ec_id; }
        }
        /// <summary>
        /// 能源名称
        /// </summary>
        public string EnergyName
        {
            set { _energyname = value; }
            get { return _energyname; }
        }
        /// <summary>
        /// 平均低位发热量
        /// </summary>
        public decimal? EnergyHot
        {
            set { _energyhot = value; }
            get { return _energyhot; }
        }
        /// <summary>
        /// 折标准煤系数
        /// </summary>
        public decimal EnergyProportion
        {
            set { _energyproportion = value; }
            get { return _energyproportion; }
        }
        #endregion Model


        #region  成员方法

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnergyCalc(int EC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select EC_ID,EnergyName,EnergyHot,EnergyProportion ");
            strSql.Append(" FROM Bas_EnergyCalc ");
            strSql.Append(" where EC_ID=@EC_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@EC_ID", SqlDbType.Int,4)};
            parameters[0].Value = EC_ID;

            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["EC_ID"].ToString() != "")
                {
                    EC_ID = int.Parse(ds.Tables[0].Rows[0]["EC_ID"].ToString());
                }
                EnergyName = ds.Tables[0].Rows[0]["EnergyName"].ToString();
                if (ds.Tables[0].Rows[0]["EnergyHot"].ToString() != "")
                {
                    EnergyHot = decimal.Parse(ds.Tables[0].Rows[0]["EnergyHot"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EnergyProportion"].ToString() != "")
                {
                    EnergyProportion = decimal.Parse(ds.Tables[0].Rows[0]["EnergyProportion"].ToString());
                }
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("Bas_EnergyCalc", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Bas_EnergyCalc(");
            strSql.Append("EnergyName,EnergyHot,EnergyProportion)");
            strSql.Append(" values (");
            strSql.Append("@EnergyName,@EnergyHot,@EnergyProportion)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@EnergyName", SqlDbType.NVarChar,50),
					new SqlParameter("@EnergyHot", SqlDbType.Decimal,9),
					new SqlParameter("@EnergyProportion", SqlDbType.Decimal,9)};
            parameters[0].Value = EnergyName;
            parameters[1].Value = EnergyHot;
            parameters[2].Value = EnergyProportion;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Bas_EnergyCalc set ");
            strSql.Append("EnergyName=@EnergyName,");
            strSql.Append("EnergyHot=@EnergyHot,");
            strSql.Append("EnergyProportion=@EnergyProportion");
            strSql.Append(" where EC_ID=@EC_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@EC_ID", SqlDbType.Int,4),
					new SqlParameter("@EnergyName", SqlDbType.NVarChar,50),
					new SqlParameter("@EnergyHot", SqlDbType.Decimal,9),
					new SqlParameter("@EnergyProportion", SqlDbType.Decimal,9)};
            parameters[0].Value = EC_ID;
            parameters[1].Value = EnergyName;
            parameters[2].Value = EnergyHot;
            parameters[3].Value = EnergyProportion;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string EC_IDs)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bas_EnergyCalc ");
            strSql.Append(" where EC_ID in ( " + EC_IDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnergyCalc GetModel(int EC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 EC_ID,EnergyName,EnergyHot,EnergyProportion ");
            strSql.Append(" FROM Bas_EnergyCalc ");
            strSql.Append(" where EC_ID=@EC_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@EC_ID", SqlDbType.Int,4)};
            parameters[0].Value = EC_ID;

            HsTec.Mobile.Green.SqlServerDAL.EnergyCalc model = new HsTec.Mobile.Green.SqlServerDAL.EnergyCalc();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["EC_ID"].ToString() != "")
                {
                    model.EC_ID = int.Parse(ds.Tables[0].Rows[0]["EC_ID"].ToString());
                }
                model.EnergyName = ds.Tables[0].Rows[0]["EnergyName"].ToString();
                if (ds.Tables[0].Rows[0]["EnergyHot"].ToString() != "")
                {
                    model.EnergyHot = decimal.Parse(ds.Tables[0].Rows[0]["EnergyHot"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EnergyProportion"].ToString() != "")
                {
                    model.EnergyProportion = decimal.Parse(ds.Tables[0].Rows[0]["EnergyProportion"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Bas_EnergyCalc ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

